package com.yhz.openapi.web.controller;

import com.yhz.openapi.common.ResponseResult;
import com.yhz.openapi.common.ResultTable;
import com.yhz.openapi.entity.Menu;
import com.yhz.openapi.services.MenuServices;
import com.yhz.openapi.services.RoleServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleServices roleServices;

    @Autowired
    private MenuServices menuServices;

    @RequestMapping("/getRole")
    public ResultTable getRolePageWithName(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer limit, String name){
        ResultTable rt=roleServices.getRolePageWithName(page,limit,name);
        return rt;
    }

    @RequestMapping("/updateStatus")
    public ResponseResult updateStatus(Integer id,Integer status){
        return roleServices.updateStatus(id,status);
    }


    @RequestMapping("/role_menu")
    public List<Integer> getRoleMenu(Integer roleId){
        return roleServices.getRoleMenu(roleId);
    }


    @RequestMapping("/menu_tree")
    public List<Menu> getMenuTree(){
       return menuServices.getAllMenuTree();
    }

    @RequestMapping("/assign_menu")
    public ResponseResult assignMenu(Integer roleId,Integer[] menuIds){
        try {
            return roleServices.assignMenu(roleId,menuIds);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.getFail("操作失败！");
    }
}
